import { request, API } from "~@/common/request";
import store from "@/store";

export const fetchUserInfo = (callback) => {
  var token = uni.getStorageSync("token");
  // #ifdef APP-PLUS
  if (!token) {
    return;
  }
  // #endif
  var userInfo = store.state.userInfo;
  userInfo.token = token;
  store.commit("SET_USER_INFO", userInfo);
  request({
    path: API.USER_INFO,
  }).then((userRes) => {
    console.log(userRes);
    if (!userRes.isSuccess) {
      return;
    }
    userInfo.token = uni.getStorageSync("token");
    userInfo.nickname = userRes.data.nickname;
    userInfo.user_avatar = userRes.data.user_avatar;
    userInfo.user_phone = userRes.data.user_phone;
    userInfo.vip_expired_time = userRes.data.vip_expired_time
    userInfo.is_vip = userRes.data.is_vip
    store.commit("SET_USER_INFO", userInfo);
    uni.$emit("login_success", userInfo);
    if (callback) {
      callback();
    }
  });
};

export const  logoutUser = (callback) => {
  uni.removeStorageSync("token");
  var userInfo = {
    user_avatar: "https://c-ssl.duitang.com/uploads/item/201902/17/20190217004623_htvoq.jpg",
    nickname: "未登录",
    user_phone: "",
    vip_expired_time: "0001-01-01T00:00:00Z",
    isLogin: false,
    is_vip: false,
  };
  store.commit("SET_USER_INFO", userInfo);
  if (callback) {
    callback();
  }
}
